package com.binguo.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.binguo.api.result.Result;
import com.binguo.api.form.*;
import com.binguo.api.result.ResultGenerator;
import com.binguo.api.service.RuiJingService;
import com.binguo.api.transfer.OrderNoBuilder;
import com.binguo.model.api.vo.CustomerVO;
import com.binguo.model.api.vo.CustomerWebLoginVO;
import com.binguo.model.api.vo.EmployeeVO;
import com.binguo.model.api.vo.LoginVO;
import com.binguo.service.LoginService;
import com.binguo.thirdPay.MD5Util;
import com.binguo.thirdPay.ruijingPay.RuiJingShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import java.util.List;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;


/**
 * Created by haibang on 18/5/7.
 */
@Api(tags = "收银端: 登陆")
@RestController
@RequestMapping
public class LoginController {

    private static Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private OrderNoBuilder orderNoBuilder;
    @Autowired
    LoginService loginService;
    @Autowired
    RuiJingShopService ruiJingShopService;

    @GetMapping(value = "/api/test", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取登陆页面收营员列表", notes = "通过商户id,测试shopId＝2")
    public Result test() {
        return ResultGenerator.genSuccessResult(orderNoBuilder.createPlatformOrderNo());
    }


    @GetMapping(value = "/api/login/employees", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取登陆页面收营员列表", notes = "通过商户id,测试shopId＝2")
    public Result getEmployees(@ApiParam("商户id") @RequestParam @NotNull Integer shopId) {
        List<EmployeeVO> list = loginService.findEmployees(shopId);
        return ResultGenerator.genSuccessResult(list);
    }

    @PostMapping(value = "/api/login", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "登陆", notes = "通过手机号,密码，测试密码：test")
    public Result login(@RequestBody @Valid LoginForm form) {
        LoginVO loginVO = loginService.login(form.getPhone(), form.getPassword(), form.getDeviceId(), form.getCashierDeskId());
        return ResultGenerator.genSuccessResult(loginVO);
    }

    @PostMapping(value = "/api/web/login", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "登陆", notes = "通过手机号,密码，测试密码：test")
    public Result webLogin(@RequestBody @Valid YPCardLoginForm form) {
        CustomerWebLoginVO loginVO = loginService.webCustomerLogin(form.getPhone(),form.getPassword());
        return ResultGenerator.genSuccessResult(loginVO);
    }

    @PostMapping(value = "/api/web/registerPassword", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "登陆", notes = "通过手机号,密码，测试密码：test")
    public Result webRegisterPassword(@RequestBody @Valid WebRegisterPasswordForm form) {
        loginService.webRegisterPassword(form.getCustomerId(),form.getNewPassword(),form.getOldPassword());
        return ResultGenerator.genSuccessResult();
    }


    @PostMapping(value = "/api/ypCardlogin", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "登陆", notes = "通过手机号,密码，测试密码：test")
    public Result ypCardlogin(@RequestBody @Valid YPCardLoginForm form) {
       return ResultGenerator.genSuccessResult(loginService.ypCardLogin(form.getPhone(),form.getPassword()));
    }


    @PostMapping(value = "/api/reset/password", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改密码", notes = "通过手机号和新密码")
    public Result resetPassword(@RequestBody @Valid ResetPasswordForm form) {
        loginService.resetPassword(form.getPhone(), form.getNewPassword());
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping(value = "/api/logout", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "登出/切换用户", notes = "通过员工id和设备号")
    public Result logout(@RequestBody @Valid LogoutForm form) {
        loginService.logout(form.getEmployeeId(), form.getDeviceId());
        return ResultGenerator.genSuccessResult();
    }


    @PostMapping(value = "/api/ruiJingLogin", produces = APPLICATION_JSON_VALUE)
    @ApiOperation(value = "登陆", notes = "通过手机号,密码，测试密码：test")
    public Result ruiJingLogin(@RequestBody @Valid RuiJingLoginForm form) {
        return ResultGenerator.genSuccessResult(ruiJingShopService.ruiJingLogin(form.getDeviceId(),form.getUsernum(), form.getPassword()));
    }


}
